package com.uobgroup.ews.uam.core.dao;


import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.uobgroup.ews.uam.core.domain.Reference;
import com.uobgroup.ews.uam.core.domain.RoleSubType;

@Repository
public class RoleSubTypeDaoImpl extends AbstractJpaDao<RoleSubType> implements RoleSubTypeDao 
{
   public RoleSubTypeDaoImpl() {
	      setClazz(RoleSubType.class);
	}

	public long count() {
	      return entityManager.createQuery("SELECT COUNT(o) FROM RoleSubType o", Long.class).getSingleResult();
	   }
	
	@SuppressWarnings("unchecked")
	public List<RoleSubType> findAll(int firstResult, int maxResults)
	{
		List<RoleSubType> list = null;
		list = entityManager.createQuery("SELECT o FROM RoleSubType o", RoleSubType.class).setFirstResult(firstResult)
	            .setMaxResults(maxResults).getResultList();
		return list;
		
	}

	@SuppressWarnings("unchecked")
	public List<RoleSubType> findByRoleTypeID(Long roleTypeID)
	{
	      
		Query query = entityManager.createQuery("select r from RoleSubType r where r.roleType="+roleTypeID+"", RoleSubType.class);	  
	    List<RoleSubType> RoleSubType = query.getResultList();
	    return RoleSubType;
		
	}
	
	@SuppressWarnings("unchecked")
	public Boolean CheckIsUse(Long roleSubTypeId)
	{
		List<?> list = entityManager.createNativeQuery("SELECT r.ID FROM UAMROLE o,ROLESUBTYPE r WHERE o.ROLESUBTYPE_ID = r.ID and r.ID="+roleSubTypeId)
				.getResultList();
	   if(list.size() > 0)
		   return true;
	   else 
		   return false;
		
	}
	/**
	 * @author dongn
	 */
	@Override
   public long countRoleSubTypeThatHasRoleTypeReference(Reference roleType) {
      return entityManager.createQuery("SELECT COUNT(R) FROM RoleSubType R WHERE R.roleType = :roleType", Long.class)
                  .setParameter("roleType", roleType).getSingleResult();
   }	

}
